<template>
  <view class="container">
    <view class="navbar" :style="{ paddingTop: `${safeAreaInsets.top}px` }"></view>
    <!-- 分类导航 -->
    <view class="classification">
      <view class="classification_text">分类 :</view>
      <view>
        <scroll-view scroll-x class="category-scroll">
          <view v-for="(item, index) in categories" :key="index" class="category-item"
            :class="{ active: currentCategory === item.id }" @click="switchCategory(item.id)">
            {{ item.name }}
          </view>
        </scroll-view>
      </view>
    </view>


    <!-- 商品列表 - 卡片式布局 -->
    <view class="product-list">
      <view v-for="product in products" :key="product.id" class="product-card" @click="goToDetail(product.id)">
        <image :src="product.image" mode="aspectFill" class="product-image" />
        <view class="product-info">
          <text class="product-name">{{ product.name }}</text>
          <text class="product-price">¥{{ product.price }}</text>
          <text class="product-rating">⭐{{ product.rating }}</text>
        </view>
        <view class="add-cart-btn" @click.stop="addToCart(product)">
          +
        </view>
      </view>
    </view>

    <!-- 底部购物车栏 -->
    <view class="cart-bar">
      <view class="cart-info">
        <text class="total-price"><text style="font-size: 14px;margin-right: 6px;">¥</text>{{ cartTotal }}</text>
        <text class="item-count">{{ cartItemCount }}件商品</text>
      </view>
      <view class="view-btn">
        <button class="checkout-btn" @click="goToCart">去结算</button>
      </view>

    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        currentCategory: 1,
        categories: [{
            id: 1,
            name: '中药饮片'
          },
          {
            id: 2,
            name: '理疗器械'
          },
          {
            id: 3,
            name: '健康课程'
          }
        ],
        products: [{
            id: 1,
            name: '优质枸杞',
            price: 68.00,
            image: 'https://models.vectquant.com/3dmodels/images/product1.png',
            rating: 4.8,
            category: 1
          },
          {
            id: 2,
            name: '智能艾灸仪',
            price: 299.00,
            image: 'https://models.vectquant.com/3dmodels/images/product2.png',
            rating: 4.5,
            category: 2
          }
          // 更多商品数据...
        ],
        cartItems: [],
        safeAreaInsets: uni.getSystemInfoSync().safeAreaInsets
      }
    },
    computed: {
      cartTotal() {
        return this.cartItems.reduce((total, item) => total + item.price * item.quantity, 0).toFixed(2)
      },
      cartItemCount() {
        return this.cartItems.reduce((count, item) => count + item.quantity, 0)
      }
    },
    methods: {
      switchCategory(categoryId) {
        this.currentCategory = categoryId
        // 这里应调用API获取对应分类的商品
      },
      addToCart(product) {
        const existingItem = this.cartItems.find(item => item.id === product.id)
        if (existingItem) {
          existingItem.quantity++
        } else {
          this.cartItems.push({
            ...product,
            quantity: 1
          })
        }
        uni.showToast({
          title: '已加入购物车'
        })
      },
      goToCart() {
        uni.navigateTo({
          url: "/subpkg/mallSubpkg/shaoppingCart/shaoppingCart"
        })
      },
      goToDetail(productId) {
        uni.navigateTo({
          url: `/pages/detail/detail?id=${productId}`
        })
      }
    }
  }
</script>

<style lang="scss">
  .navbar {
    margin-top: 50px;

  }

  .classification {
    margin-left: 10px;
    display: flex;
    // justify-content: space-between;
    align-items: center;


    .classification_text {
      margin-right: 10px;
      background-color: #8B4513;
      padding: 10rpx 15rpx;
      color: white;
      border-radius: 30rpx;

    }

    .category-scroll {
      // white-space: nowrap;
      padding: 10rpx;
      display: flex;

      /* background: #fff; */
      .category-item {
        display: inline-block;
        font-size: 14px;
        padding: 10rpx 15rpx;
        margin-right: 20rpx;
        border-radius: 30rpx;
        background: #f5f5f5;
      }

      .category-item.active {
        background: #007AFF;
        color: white;
      }
    }



  }


  .product-list {
    padding: 20rpx;
  }

  .product-card {
    background: white;
    border-radius: 16rpx;
    margin-bottom: 24rpx;
    padding: 24rpx;
    display: flex;
    align-items: center;
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  }

  .product-image {
    width: 160rpx;
    height: 160rpx;
    border-radius: 12rpx;
  }

  .product-info {
    flex: 1;
    margin-left: 24rpx;
  }

  .product-name {
    font-size: 32rpx;
    font-weight: 500;
    display: block;
  }

  .product-price {
    color: #FF5000;
    font-size: 36rpx;
    font-weight: bold;
    display: block;
    margin-top: 12rpx;
  }

  .product-rating {
    color: #999;
    font-size: 24rpx;
    display: block;
    margin-top: 8rpx;
  }

  .add-cart-btn {
    width: 60rpx;
    height: 60rpx;
    border-radius: 50%;
    background: #007AFF;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 36rpx;
  }

  .cart-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: white;
    padding: 20rpx 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-shadow: 0 -4rpx 12rpx rgba(0, 0, 0, 0.1);
  }

  .total-price {
    color: red;
    margin-right: 20px;
    font-size: 20px;
  }

  .checkout-btn {
    background: #007AFF;
    color: white;
    border-radius: 40rpx;
    padding: 5rpx 20rpx;
  }
</style>